package com.china08.yxyapi.api.manage.sch.office.innernotice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 学校管理员 内部通知 发件箱 列表查询
 * 
 */
@CrossOrigin
@RestController("SchInnernoticeSendSrhController")
public class InnernoticeSendSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/innernotice/send/srh", method = RequestMethod.POST)
    public Page<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        // 当前教育局id
        String schoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String userId = getProfile(request, response).getId();
        // 未删除状态
        int deleteStatus = 0;
        // 发件人列表
        Page<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndUserIdAndDeleteStatus(schoolId, userId, deleteStatus, pageable);

        List<RespModel> respModelLst = new ArrayList<RespModel>();
        for (YxyNoticeInner yxyNoticeInner : page.getContent()) {
            RespModel respModel = new RespModel();
            // JAVA 8 查询所有接受用户信息.多个用户以逗号分割，出现异常，返回空字符串
            respModel.receiverUserNick = yxyNoticeInner.getAllReceiverUserNick(",");
            // 该通知已收到人数
            respModel.readManCount = yxyNoticeInner.getReadManCount(1);
            // 该通知未收到人员列表拼接字符串
            respModel.readManUserNicknameStr = yxyNoticeInner.getReadManUserNicknameStr(0, "、 ");
            respModel.yxyNoticeInner = yxyNoticeInner;
            respModel.createdDate = DateToolUtils.format(yxyNoticeInner.getCreatedDate(), "yyyy-MM-dd");
            respModelLst.add(respModel);
        }
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());
        return respModelPage;
    }

    public static class ReqModel extends YxyApiPageReqModel {

    }

    public static class RespModel {
        // 消息的全部信息
        public YxyNoticeInner yxyNoticeInner;
        // 发布时间
        public String createdDate;
        // 接收用户名称
        public String receiverUserNick;
        // 已收到人数
        public long readManCount;
        // 未收到人员的姓名列表 多个用户昵称使用 、+ 空格 分隔
        public String readManUserNicknameStr;
    }
}
